import 'package:flutter/material.dart';

class BaseSelectTriangleBlock extends StatelessWidget {
  final double colorBlockSize;
  final Color bgColor;
  final double radius;
  final Color tickColor;

  const BaseSelectTriangleBlock({
    Key key,
    @required this.colorBlockSize,
    this.radius = 4,
    this.bgColor = const Color(0xFFFF004F),
    this.tickColor = Colors.white,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(colorBlockSize, colorBlockSize),
      painter: BaseSelectTriangleBlockPainter(bgColor: this.bgColor, radius: this.radius, tickColor: this.tickColor),
    );
  }
}

class BaseSelectTriangleBlockPainter extends CustomPainter {
  final Color bgColor;
  final double radius;
  final Color tickColor;

  BaseSelectTriangleBlockPainter({this.radius = 4, this.bgColor = const Color(0xFFFF004F), this.tickColor = Colors.white});

  @override
  void paint(Canvas canvas, Size size) {
    Path _path = Path();
    _path..moveTo(size.width, 0);
    _path..lineTo(size.width, size.height);
    _path..lineTo(0, size.height);
    _path..close();
    canvas.clipPath(_path);
    _drawRectangle(canvas, size);
    _drawTick(canvas, size);
  }

  ///绘制矩形
  _drawRectangle(Canvas canvas, Size size) {
    canvas.save();
    Paint _paint = Paint()
      ..color = this.bgColor
      ..strokeWidth = 2
      ..style = PaintingStyle.fill;
    Path _path = Path();
    _path..moveTo(size.width, 0);
    _path..lineTo(size.width, size.height - 4);
    _path..arcToPoint(Offset(size.width - 4, size.height), radius: Radius.circular(this.radius), largeArc: false);
    _path.lineTo(0, size.height);
    _path..close();
    canvas.drawPath(_path, _paint);
    canvas.restore();
  }

  ///绘制勾
  _drawTick(Canvas canvas, Size size) {
    canvas.save();
    Paint _paint = Paint()
      ..color = this.tickColor
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;
    Path _path = Path();
    _path.moveTo(size.width / 3, size.height * 4 / 5);
    _path..lineTo(size.width * 3 / 5, size.height * 0.9);
    _path..lineTo(size.width * 0.9, size.height * 2 / 5);
    canvas.drawPath(_path, _paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(_) => false;
}
